import React, { Component } from 'react';
import { base, ajax, toast, viewModel } from 'nc-lightapp-front';
const { NCModal, NCButton, NCRadio, NCTextArea, NCSelect } = base;
const { getGlobalStorage } = viewModel
const NCOption = NCSelect.NCOption;
import classnames from 'classnames';
import { requestPromise } from 'ssccommon/utils/asyncFunctions.js';
import './index.less'

class RejectModalCtrl extends Component {
    constructor(props) {
        super();
        this.state = {
            multiLang: {},
            isShow: false,
            actid: '',
            repeat: '1',
            ruleSearchValue: '',

            operationData: null,
            showradio: false,
            ncoptions: null,
            defaultOption: null
        }
        this.ruleSearchThresholdTime = null;

        this.multiLangPromise = window.__SSC_MultiLangPromise;
        this.multiLangPromise.then((multiLang) => {
            this.setState({
                multiLang
            })
        })
    }

    save = async () => {
        let multiLang = await this.multiLangPromise;
        let actid = this.state.actid;;
        let oneOpt = this.state.ncoptions.find((oneOpt) => {
            return oneOpt.value == actid
        })
        let actname = oneOpt && oneOpt.name;

        let reqParam = {
            memo: this.props.handleReason,
            actid: actid,
            actname,
            repeat: this.state.repeat,
        };

        if (!actid) {
            toast({
                color: 'warning',
                title: multiLang['job-approve-qzy'],// 请注意
                content: multiLang['job-approve-qxzbhlc']//请选择驳回流程
            })
            return
        }
        if (!this.props.handleReason) {
            toast({
                color: 'warning',
                title: multiLang['job-approve-qzy'],//请注意
                content: multiLang['job-approve-qsrbhyy']//请输入驳回原因
            })
            return
        }

        this.setState({
            isShow: false
        })

        this.props.confirmEvent && this.props.confirmEvent(reqParam);
    }

    cancel = () => {
        this.setState({
            isShow: false,
            actid: ''
        });
    }

    handleChange = (value) => {
        this.setState({
            actid: value
        })
    }

    handleRadio = (value) => {
        this.setState({
            repeat: value
        })
    }

    ruleItemSelect = (item, selectedRuleItems) => {
        if (selectedRuleItems[item.refpk]) {
            delete selectedRuleItems[item.refpk]
        } else {
            selectedRuleItems[item.refpk] = item;
        }

        this.props.setCheckedCredits(Object.values(selectedRuleItems));
    }

    ruleSearchChange(e) {
        let value = e.target.value;
        let trimValue = value.trim();
        this.setState({
            ruleSearchValue: value
        });
        clearTimeout(this.ruleSearchThresholdTime);
        this.ruleSearchThresholdTime = setTimeout(() => {

            let creditRuleItems = this.props.creditRuleItems;
            Object.values(creditRuleItems).forEach((rootItem) => {
                rootItem.children.forEach((item) => {
                    if (!trimValue || item.refname.indexOf(trimValue) != -1) {
                        item.notSearch = false;
                    } else {
                        item.notSearch = true;
                    }
                })
            })

            this.props.setCreditRuleItems(creditRuleItems);
        }, 200)


    }

    show = async (currentGridData) => {
        this.setState({
            isShow: true
        })

        try {
            //获取驳回初始化信息
            const result = await requestPromise({
                url: '/nccloud/ssctp/sscbd/SSCTaskGetRejectActivityAction.do',
                data: currentGridData
            });

            if (result.success) {
                this.setState({
                    showRadio: result.data.showradio,
                    ncoptions: result.data.NCOptions,
                    defaultOption: result.data.default
                })
            }
        } catch (e) {
            toast({ title: e.message, duration: 5, color: 'danger', })
        }
    }

    componentwillreceiveprops(nextProps) {
        
    }

    render() {
        let multiLang = this.state.multiLang;
        let {
            creditsArray,
            setCheckedCredits,
            creditView
        } = this.props
        let ncoptions = this.state.ncoptions;
        let getNCOptions = () => {
            let refHtml = [];
            if (ncoptions != null && ncoptions != "" && ncoptions != '') {
                ncoptions.map((one) => {
                    refHtml.push(<NCOption value={one.value}>{one.name}</NCOption>)
                })
            }
            return refHtml
        }

        let selectedRuleItems = {}
        creditsArray && creditsArray.forEach((item) => {
            selectedRuleItems[item.refpk] = item;
        })

        return (
            <NCModal  fieldid="reject" show={this.state.isShow} id="RejectModalCtrl" onHide={() => { this.cancel() }}>
                <NCModal.Header closeButton={true}>
                    {/*驳回*/}
                    <NCModal.Title>{multiLang['job-approve-bh']}</NCModal.Title>
                </NCModal.Header>
                <NCModal.Body>
                    <div className='reject-content'>
                        <div className='reject-title--content'
                        >
                            {/*驳回至*/}
                            <span>{multiLang['job-approve-bhz']}</span>

                            <NCSelect
                                fieldid="reject-select"
                                defaultValue={this.state.defOption}
                                onChange={this.handleChange}
                            >
                                {getNCOptions()}
                            </NCSelect>

                            {/* <div className={creditView?'contentdiv-show':'contentdiv-hide'}>
                            {CreditRuleTreeRef({
                                onChange: setCheckedCredits,
                                clickContainer: <span>信用评价</span>,
                                isMultiSelectedEnabled: true
                            })}
                            </div> */}
                        </div>
                        <div className='credits-box'>
                            <div className='credits-show clearfix'>
                                {
                                    creditsArray && creditsArray.map((each, idx) => (
                                        <span className='item-credit-view'>
                                            {each.refname}
                                            <i
                                                onClick={() => {
                                                    this.props.deleteCreditByIdx(idx)
                                                }}
                                            > - </i>
                                        </span>
                                    ))
                                }
                            </div>
                            {/*请输入驳回原因*/}
                            <NCTextArea
                                fieldid="reject-reason"
                                placeholder={multiLang['job-approve-qsrbhyy']}
                                className=""
                                showMax={true}
                                max={100}
                                value={this.props.handleReason}
                                onChange={(val) => {
                                    this.props.setHandleReason(val);
                                }}
                            />
                        </div>
                    </div>

                </NCModal.Body>
                <NCModal.Footer>
                    {this.state.showRadio ? (
                        <NCRadio.NCRadioGroup
                            selectedValue={this.state.repeat}
                            onChange={this.handleRadio}
                            className='footer-radio-group'
                        >
                            {/*重走流程*/}
                            <NCRadio value="1" >{multiLang['job-approve-czlc']}</NCRadio>
                            {/* 不重走流程 */}
                            <NCRadio value="2" >{multiLang['job-approve-bczlc']}</NCRadio>
                        </NCRadio.NCRadioGroup>
                    ) : ("")}


                    <NCButton fieldid="reject" colors="primary" onClick={this.save} >{multiLang['job-approve-bh']}</NCButton>
                    <NCButton fieldid="revoke-approval" onClick={this.cancel}>{multiLang['job-common-qx']}</NCButton>
                </NCModal.Footer>
            </NCModal>
        )
    }
}

export default RejectModalCtrl;